package com.app.utils;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

/**
 * 
 * 
 */
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration{

	private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
	private Drawable mDivider;
	private int mDividerHeight = 0;
	private int mPaddingLeft = 0;
	private int mPaddingRight = 0;
	private  Context mContext;
	
	public DividerGridItemDecoration(Context context){
		mContext = context;
		final TypedArray a = context.obtainStyledAttributes(ATTRS);
		mDivider = a.getDrawable(0);
		a.recycle();
	}

	/**
	 * 
	 * 在itemView绘制完成之前调用，也就是说此方法draw出来的效果将会在itemView的下面
	 * */
	@Override
	public void onDraw(Canvas c, RecyclerView parent, State state){
		drawHorizontal(c, parent);
		drawVertical(c, parent);
	}
	
	/**
	 * 
	 * 与onDraw相反，draw出来的效果将叠加在itemView的上面
	 * */
	@Override
	public void onDrawOver(Canvas c, RecyclerView parent, State state) {
		super.onDrawOver(c, parent, state);
	}
	
	private int getSpanCount(RecyclerView parent){
		// 网格的跨度
		int spanCount = -1;
		LayoutManager layoutManager = parent.getLayoutManager();
		if (layoutManager instanceof GridLayoutManager)	{
			spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
		} else if (layoutManager instanceof StaggeredGridLayoutManager)	{
			spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
		}
		else if(layoutManager instanceof  LinearLayoutManager){
			spanCount = 1;
		}
		return spanCount;
	}

	
	public void setDividerHeight(int dpheight){
		mDividerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpheight, mContext.getResources().getDisplayMetrics());
	}
		
	public void setDividerPadding(int paddingLeft,int paddingRight){
		mPaddingLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingLeft, mContext.getResources().getDisplayMetrics());
		mPaddingRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingRight, mContext.getResources().getDisplayMetrics());
	}
	
	private  void drawHorizontal(Canvas c, RecyclerView parent){
		int childCount = parent.getChildCount();
		for (int i = 0; i < childCount; i++){
			final View child = parent.getChildAt(i);
			final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
					.getLayoutParams();
			final int left = child.getLeft() - params.leftMargin;
			final int right = child.getRight() + params.rightMargin
					+ mDivider.getIntrinsicWidth();
			final int top = child.getBottom() + params.bottomMargin;
			final int bottom = top + mDivider.getIntrinsicHeight();
			mDivider.setBounds(left+mPaddingLeft, top, right-mPaddingRight, bottom+mDividerHeight);
			mDivider.draw(c);
		}
	}

	
	private void drawVertical(Canvas c, RecyclerView parent){
		final int childCount = parent.getChildCount();
		for (int i = 0; i < childCount; i++){
			final View child = parent.getChildAt(i);
			final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
			final int top = child.getTop() - params.topMargin;
			final int bottom = child.getBottom() + params.bottomMargin;
			final int left = child.getRight() + params.rightMargin;
			final int right = left + mDivider.getIntrinsicWidth();
			mDivider.setBounds(left, top+mPaddingLeft, right+mDividerHeight, bottom-mPaddingRight);
			mDivider.draw(c);
		}
	}

	private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
			int childCount)	{
		LayoutManager layoutManager = parent.getLayoutManager();
		if (layoutManager instanceof GridLayoutManager){
			if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
			{
				return true;
			}
		} else if (layoutManager instanceof StaggeredGridLayoutManager){
			int orientation = ((StaggeredGridLayoutManager) layoutManager)
					.getOrientation();
			if (orientation == StaggeredGridLayoutManager.VERTICAL)
			{
				if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
				{
					return true;
				}
			} else{
				childCount = childCount - childCount % spanCount;
				if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
					return true;
			}
		}else if (layoutManager instanceof LinearLayoutManager) {
			if(pos == childCount-1){
				return true;
			}
		}
		return false;
	}

	/**
	 * 是不是最后一个Itemview
	 * @param parent
	 * @param pos
	 * @param spanCount
	 * @param childCount
	 * @return
	 */
	private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
			int childCount){
		LayoutManager layoutManager = parent.getLayoutManager();
		if (layoutManager instanceof GridLayoutManager)	{
			childCount = childCount - childCount % spanCount;
			if (pos >= childCount)// 如果是最后一行，则不需要绘制底部
				return true;
		} else if (layoutManager instanceof StaggeredGridLayoutManager){
			int orientation = ((StaggeredGridLayoutManager) layoutManager)	.getOrientation();
			if (orientation == StaggeredGridLayoutManager.VERTICAL)	{
				// StaggeredGridLayoutManager 且纵向滚动
				childCount = childCount - childCount % spanCount;
				// 如果是最后一行，则不需要绘制底部
				if (pos >= childCount)
					return true;
			} else{
				// StaggeredGridLayoutManager 且横向滚动
				// 如果是最后一行，则不需要绘制底部
				if ((pos + 1) % spanCount == 0){
					return true;
				}
			}
		}else if (layoutManager instanceof LinearLayoutManager) {
			if(pos == childCount-1){
				return true;
			}
		}
		return false;
	}

	/**
	 * 计算通过配置outRect来设置itemView的inset边界，相当于设置itemView的margin
	 * */
	@Override
	public void getItemOffsets(Rect outRect, int itemPosition,
			RecyclerView parent){
		int spanCount = getSpanCount(parent);
		int childCount = parent.getAdapter().getItemCount();
	
		if (isLastRaw(parent, itemPosition, spanCount, childCount))	{
			// 如果是最后一行，则不需要绘制底部
			outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
		} else if (isLastColum(parent, itemPosition, spanCount, childCount)){
			// 如果是最后一列，则不需要绘制右边
			outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
		} else{
			outRect.set(0, 0, mDivider.getIntrinsicWidth(),
					mDivider.getIntrinsicHeight()+mDividerHeight);
//			LayoutManager layoutManager = parent.getLayoutManager();
//			if (layoutManager instanceof GridLayoutManager)	{
//				
//			}else if(layoutManager instanceof StaggeredGridLayoutManager){
//				
//			}else if(layoutManager instanceof LinearLayoutManager){
//				int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
//				if(orientation == LinearLayoutManager.VERTICAL){
//					outRect.set(0, 0, mDivider.getIntrinsicWidth(),
//							mDivider.getIntrinsicHeight()+mDividerHeight);
//				}else {
//					outRect.set(0, 0, mDivider.getIntrinsicWidth()+mDividerHeight,
//							mDivider.getIntrinsicHeight());
//				}
//			}
		}
	}
}
